package com.ruoyi.supplier.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.supplier.domain.bo.StockRecordBo;
import com.ruoyi.supplier.domain.vo.StockRecordVo;
import com.ruoyi.supplier.domain.StockRecord;
import com.ruoyi.supplier.mapper.StockRecordMapper;
import com.ruoyi.supplier.service.IStockRecordService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 出入库记录Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-18
 */
@RequiredArgsConstructor
@Service
@DS("slave")
public class StockRecordServiceImpl implements IStockRecordService {

    private final StockRecordMapper baseMapper;

    /**
     * 查询出入库记录
     */
    @Override
    public StockRecordVo queryById(Integer stockRecordId){
        return baseMapper.selectVoById(stockRecordId);
    }

    /**
     * 查询出入库记录列表
     */
    @Override
    public TableDataInfo<StockRecordVo> queryPageList(StockRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<StockRecord> lqw = buildQueryWrapper(bo);
        Page<StockRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询出入库记录列表
     */
    @Override
    public List<StockRecordVo> queryList(StockRecordBo bo) {
        LambdaQueryWrapper<StockRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<StockRecord> buildQueryWrapper(StockRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<StockRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getStockId() != null, StockRecord::getStockId, bo.getStockId());
        lqw.eq(bo.getProductSkuId() != null, StockRecord::getProductSkuId, bo.getProductSkuId());
        lqw.eq(bo.getType() != null, StockRecord::getType, bo.getType());
        lqw.eq(bo.getShouldNum() != null, StockRecord::getShouldNum, bo.getShouldNum());
        lqw.eq(bo.getStockNum() != null, StockRecord::getStockNum, bo.getStockNum());
        return lqw;
    }

    /**
     * 新增出入库记录
     */
    @Override
    public Boolean insertByBo(StockRecordBo bo) {
        StockRecord add = BeanUtil.toBean(bo, StockRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setStockRecordId(add.getStockRecordId());
        }
        return flag;
    }

    /**
     * 修改出入库记录
     */
    @Override
    public Boolean updateByBo(StockRecordBo bo) {
        StockRecord update = BeanUtil.toBean(bo, StockRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(StockRecord entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除出入库记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Integer> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
